package com.mall.order.biz.handler;

import com.mall.order.biz.context.CreateOrderContext;
import com.mall.order.biz.context.TransHandlerContext;
import com.mall.order.mq.delay.DelayOrderCancelProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

/**
 * @Description: 利用mq发送延迟取消订单消息
 * @Date: 2019-09-17 23:14
 **/
@Component
@Slf4j
public class SendMessageHandler extends AbstractTransHandler {

    @Autowired
    DelayOrderCancelProducer producer;

    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {

        //当执行到这个handler的时候，整个下单操作已经完成
        //每次下单后发送一条延时消息到RocketMQ，具体时长根据业务需求（测试时可选取短时长）
        CreateOrderContext createOrderContext = (CreateOrderContext) context;
        String orderId = createOrderContext.getOrderId();
        return producer.sendDelayCancelOrderMassage(orderId);
    }
}